define(['jquery'], function($) {
	/*
	 第一个参数：在哪个元素上面覆盖层
	 第二个参数：哪个元素是显示出来的
	 第三个参数：点击弹层除了弹层显示之后执行的回调
	 注意：传进来的元素必须是有position：relative或者absolute才能显示
	 * */
	var Layer = function(fatherLayer,sonLayer,fn) {
		this.fatherLayer = fatherLayer;
		this.sonLayer = sonLayer;
		this.fn = fn;
		this.create();
	}
	Layer.prototype.create = function() {
		if($('.myLayer').length==0){
			var tpl = '<div class="myLayer" style="z-index:9999;width: 100%;height: 100%;background: black;opacity: 0.7;position: absolute;left: 0px;top: 0px;"></div>';
			var tplObj = $(tpl);
			this.fatherLayer.children().eq(0).before(tplObj);
		}
		this.zIndexList = [];
		if(this.sonLayer instanceof Array){
			for(var i=0;i<this.sonLayer.length;i++){
				var item = this.sonLayer[i];
				if(item.css('z-index')!=undefined){
					this.zIndexList.push(item.css('z-index'));
				}else{
					this.zIndexList.push(0);
				}
				item.css('z-index', 10000);
			}
		}else{
			this.sonLayer.css('z-index', 10000);
		}
		this.activeLayer = $('.myLayer');
		var that = this;
		this.activeLayer.on('click',function(){
			this.remove();
			that.fn();
			that = null;
		});
	}
	Layer.prototype.remove = function(){
		for(var i=0;i<this.sonLayer.length;i++){
				var item = this.sonLayer[i];
				item.css('z-index', this.zIndexList[i]);
			}
		this.activeLayer.remove();
		that = null;
	}
	return Layer;
});